﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Finance.Entites;
using Finance.Service.Interfaces;
using Finance.Service.Factory;
using Finance.UI.UserControls;
using Finance.UI.ViewModels;

namespace Finance.UI
{
    public partial class ChargeDetailPage : PhoneApplicationPage, INotifyPropertyChanged
    {
        #region Const Values

        private const string PAYOUTHEADER = "Payout";
        private const string INCOMEHEADER = "Income";

        #endregion

        #region Fields and Properties

        public event PropertyChangedEventHandler PropertyChanged;

        private EventHandler<SelectItemInformationEventArgs> selectorItemEventHandler;

        private PopupStatus popupSelectorStatus;
        public PopupStatus PopupSelectorStatus
        {
            get { return this.popupSelectorStatus; }
            private set
            {
                this.popupSelectorStatus = value;
                this.PropertyChanged(this, new PropertyChangedEventArgs("PopupSelectorStatus"));
            }
        }

        public object selectorDataContext;
        public object SelectorDataContext
        {
            get
            {
                return this.selectorDataContext;
            }
            private set
            {
                this.selectorDataContext = value;
                this.PropertyChanged(value, new PropertyChangedEventArgs("SelectorDataContext"));
            }
        }

        #endregion

        #region Constructor and Initial Data Actions

        public ChargeDetailPage()
        {

            InitializeComponent();
            Loaded += (o, e) => { InitializeNotifyProperties(o, e); };
            Loaded += (o, e) => { InitializeDataContext(); };
            Loaded += (o, e) => { InitializeAnimationStatus(); };
        }

        private void InitializeNotifyProperties(object sender, RoutedEventArgs e)
        {
            this.PropertyChanged += (o, evt) =>
            {
                if (/*evt.PropertyName == "PopupSelectorStatus"
                    || */evt.PropertyName == "SelectorDataContext")
                {
                    this.Selector.DataContext = o;
                    this.PopupSelectorStatus = PopupStatus.Expand;
                }
            };
            this.PropertyChanged += (o, evt) =>
            {
                if (evt.PropertyName == "PopupSelectorStatus")
                {
                    SelectorChangedBehavior();
                }
            };
            this.PropertyChanged = this.PropertyChanged ?? new PropertyChangedEventHandler((o, evt) => { });
        }

        private void InitializeDataContext()
        {
            var operation = OperationType.Unknown;
            try
            {
                operation = (OperationType)Enum.Parse(
                    typeof(OperationType),
                    NavigationContext.QueryString["operation"],
                    true);
            }
            catch { }

            if (operation == OperationType.Unknown) { return; }
            switch (operation)
            {
                case OperationType.Create:
                    {
                        App.CurrentPayoutCharge = App.ChargeDetailAssitantor.InitialPayoutChargeDetail();
                        App.CurrentIncomeCharge = App.ChargeDetailAssitantor.InitialIncomeChargeDetail();
                        break;
                    }
                case OperationType.Edit:
                    {
                        //TODO: Edit action
                        break;
                    }
                default: { break; }
            }

            ChargeDetailItemViewModel payoutModel = new ChargeDetailItemViewModel(PAYOUTHEADER, App.CurrentPayoutCharge);
            ChargeDetailItemViewModel incomeModel = new ChargeDetailItemViewModel(INCOMEHEADER, App.CurrentIncomeCharge);

            this.DataContext = new ChargeDetailPageViewModel(payoutModel, incomeModel);
        }

        private void InitializeAnimationStatus()
        {
            this.ShowSelector_Story.Completed += (o, e) =>
            {
                this.popupSelectorStatus = PopupStatus.Expand;
            };
            this.HideSelector_Story.Completed += (o, e) =>
            {
                this.popupSelectorStatus = PopupStatus.Compressed;
            };
        }

        #endregion

        #region Select Items Event Handlers

        #region Payout Charge Items Event Handlers

        private void txbPayoutClassification_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbPayoutClassificationGroup.Text = ssiE.SelectedItemHeaderName;
                    txbPayoutClassification.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Payout Classification Selector",
                txbPayoutClassification.Text,
                DataContextBuilder.InitialClassifications<PayoutClassification>(
                    App.PayoutClassificationDataStore).ToArray());
        }

        private void txbPayoutAccount_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbPayoutAccount.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Account Selector",
                txbPayoutAccount.Text,
                DataContextBuilder.InitialAccounts().ToArray());
        }

        private void txbPayoutSeller_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbPayoutSeller.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Trader Selector",
                txbPayoutSeller.Text,
                DataContextBuilder.InitialTraders().ToArray());
        }

        private void txbPayoutProject_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbPayoutProject.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Project Selector",
                txbPayoutProject.Text,
                DataContextBuilder.InitialProjects().ToArray());
        }


        #endregion

        #region Income Charge Items Event Handlers

        private void txbIncomeClassification_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbIncomeClassificationGroup.Text = ssiE.SelectedItemHeaderName;
                    txbIncomeClassification.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Income Classification Selector",
                txbIncomeClassification.Text,
                DataContextBuilder.InitialClassifications<IncomeClassification>(
                App.IncomeClassificationDataStore).ToArray());
        }

        private void txbIncomeAccount_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbIncomeAccount.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Account Selector",
                txbIncomeAccount.Text,
                DataContextBuilder.InitialAccounts().ToArray());
        }

        private void txbIncomeProject_GotFocus(object sender, EventArgs e)
        {
            selectorItemEventHandler =
                (o, ssiE) =>
                {
                    txbIncomeProject.Text = ssiE.SelectedItemName;
                };
            this.SelectorDataContext = new SelectorPageViewModel(
                "Project Selector",
                txbIncomeProject.Text,
                DataContextBuilder.InitialProjects().ToArray());
        }

        #endregion

        #region Application Bar Button Event Handlers

        private void ApplicationBarIconSaveButton_Click(object sender, EventArgs e)
        {
            //TODO save charge action
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ApplicationBarIconBackButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(
                "Do you want back to main menu withou saving?",
                "Warning",
                MessageBoxButton.OKCancel).Equals(MessageBoxResult.OK))
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

        #endregion

        #region Private Methods

        private void OnItemSelected_Click(object sender, SelectItemInformationEventArgs e)
        {
            if (selectorItemEventHandler != null)
            {
                selectorItemEventHandler(sender, e);
            }
            this.PopupSelectorStatus = PopupStatus.Compressed;
        }

        private void SelectorChangedBehavior()
        {
            switch (this.PopupSelectorStatus)
            {
                case PopupStatus.Compressed:
                    {
                        HideSelector_Story.Begin();
                        break;
                    }
                case PopupStatus.Expand:
                    {
                        ShowSelector_Story.Begin();
                        break;
                    }
                default:
                    {
                        HideSelector_Story.Begin();
                        break;
                    }
            }
        }

        #endregion
    }
}
